
/**
 * @ProjectName: 
 * @Copyright: 2014 lisheng  All Right Reserved.
 * @address: toughheart@163.com
 * @date: 2016年2月10日 下午7:42:15
 * @Description: 本内容未经本人允许禁止使用、转发.
 */
 
package com.ls.fw.mvc.springmvc.support.view.json;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.ls.fw.commons.spring.SpringContextHolder;
import com.ls.fw.mvc.springmvc.support.convert.CustomObjectMapper;


/**
 * 
 * @author lisheng
 * @date 2016年2月10日 下午7:42:15
 * @version V1.0 
 */
public class JsonView extends MappingJackson2JsonView{
	
	/**
	 * 从单个key中取出值返回，即不返回key
	 */
	private boolean extractValueFromSingleKeyModel = false;

	/**
	 *  从单个key中取出值返回，即不返回key
	 */
	public static final String _BLANK = "_blank";
	//排除的属性
	private Set<String> excludeKeys;

	public JsonView() {
		super();
		this.setContentType(DEFAULT_CONTENT_TYPE);
		CustomObjectMapper objectMapper = SpringContextHolder.getBean(CustomObjectMapper.class);
		if(objectMapper!=null){
			this.setObjectMapper(objectMapper);
		}
	}
	
	@Override
	public Object filterModel(Map<String, Object> model) {
		Map<String, Object> resultMap = new HashMap<String, Object>(model.size());
		Set<String> modelKeys = (!CollectionUtils.isEmpty(this.getModelKeys()) ? this.getModelKeys() : model.keySet());
		for (Map.Entry<String, Object> entry : model.entrySet()) {
			if (!(entry.getValue() instanceof BindingResult) && modelKeys.contains(entry.getKey()) &&
					!entry.getKey().equals(JsonView.class.getName())) {
				if((excludeKeys==null || excludeKeys.isEmpty())
				   || (!this.excludeKeys.contains(entry.getKey()))){
					resultMap.put(entry.getKey(), entry.getValue());
					if(_BLANK.equals(entry.getKey())){
						this.setExtractValueFromSingleKeyModel(true);
					}
				}
			}
		}
		return  (this.extractValueFromSingleKeyModel && resultMap.size() == 1 ? resultMap.values().iterator().next() : resultMap);
	}
	

	public Set<String> getExcludeKeys() {
		return excludeKeys;
	}


	/**
	 * 不被json的属性
	 * @author lisheng
	 * @date 2016年2月10日 下午7:48:48
	 * @version V1.0
	 * @param excludeKeys
	 */
	public JsonView setExcludeKeys(Set<String> excludeKeys) {
		this.excludeKeys = excludeKeys;
		return this;
	}
	/**
	 * 不被json的属性
	 * @author lisheng
	 * @date 2016年2月10日 下午7:48:14
	 * @version V1.0
	 * @param excludeKey
	 */
	public JsonView setExcludeKeys(String excludeKey) {
		this.excludeKeys = Collections.singleton(excludeKey);
		return this;
	}
	
	
	public JsonView setExcludeKeys(String ...excludeKey) {
		return this.addExcludeKey(excludeKey);
	}
	
	
	public JsonView addExcludeKey(String ...excludeKey) {
		if(excludeKeys==null){
			this.excludeKeys = new HashSet<String>();
		}
		if(excludeKey!=null){
			for (String string : excludeKey) {
				this.excludeKeys.add(string);
			}
		}
		return this;
	}

	public JsonView addModelKey(String ...modelKey) {
		if(getModelKeys()==null){
			this.setModelKeys(new HashSet<String>());
		}
		if(modelKey!=null){
			for (String string : modelKey) {
				this.getModelKeys().add(string);
			}
		}
		return this;
	}

	public JsonView setModelKey(String ...modelKey) {
		return this.addModelKey(modelKey);
	}
	
	/**
	 *  从单个key中取出值返回，即不返回key
	 */
	@Override
	public void setExtractValueFromSingleKeyModel(
			boolean extractValueFromSingleKeyModel) {
		super.setExtractValueFromSingleKeyModel(extractValueFromSingleKeyModel);
		this.extractValueFromSingleKeyModel = extractValueFromSingleKeyModel;
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(getClass().getName());
		if (getBeanName() != null) {
			sb.append(": name '").append(getBeanName()).append("'");
		}
		else {
			sb.append(": unnamed");
		}
		return sb.toString();
	}
}
